//
//  PredicateManager.swift
//  MetaU
//
//  Created by PerhapYs on 2022/4/12.
//

import Foundation

extension String{
    
    /// 正则验证邮箱格式
    /// - Returns: ~
    func isEmail() -> Bool{
        
        let emailRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
        let emailPre = NSPredicate.init(format: "SELF MATCHES %@", emailRegex)
        return emailPre.evaluate(with: self)
    }
    
    /// 正则验证手机号码格式
    /// - Returns: ～
    func isMobile() -> Bool{
        
        let phoneRegex = "^[1][3,4,5,7,8,9][0-9]{9}$"
        let phonePre = NSPredicate.init(format: "SELF MATCHES %@", phoneRegex)
        return phonePre.evaluate(with: self)
    }
    
    /// 验证验证码，6位长度数字
    /// - Returns: ~
    func isVCode() -> Bool{
        
        return self.isMatchLength(of: 6)
    }
    
    /// 长度验证
    /// - Parameter length: 需要验证的长度
    /// - Returns: 是否匹配
    func isMatchLength(of length:Int) -> Bool{
        
        //纯数字，固定长度判断
//         let phoneRegex = "(?<![a-zA-Z0-9])([a-zA-Z0-9]{" + length.toString() + "})(?![a-zA-Z0-9])"
        let phoneRegex = "(?<![0-9])([0-9]{" + length.toString() + "})(?![0-9])"
        let phonePre = NSPredicate.init(format: "SELF MATCHES %@", phoneRegex)
        return phonePre.evaluate(with: self)
    }
    
    /// 是否为账号
    /// - Returns: ～
    func isAccount() -> Bool{
        
        return !self.isEmpty
    }
    
    /// 是否为密码
    /// - Returns: ～
    func isPassword() -> Bool{
        
        return self.length >= 6
    }
}
